app.controller('CookListCtrl',['$rootScope','$scope','CookApi','ngTableParams','sweetAlertUtil', function($rootScope,$scope, CookApi, ngTableParams, sweetAlertUtil){
    var vm = this;
    CookApi.getRoleInfo().then(function(res){
        if(res.code == 200){
            vm.role = res.result.role_tag;
        }
    });

    $scope.cooksQueryParam = {
		page: 1
	}
	$scope.cooksTable = new ngTableParams({
        page: 1,
        count:20 
    }, {
        counts:[],
        getData: function ($defer, params) {
            $scope.cooksQueryParam.page=params.page() ;
            $scope.loaded = false;
            CookApi.getCookList($scope.cooksQueryParam).then(function(res){
            	if(res.code ==200){
                    $scope.loaded = true;
	                $scope.cooksTable.total(res.result.total);
	                $defer.resolve(res.result.data);
            	}
            });
        }
    });
    vm.citys = [];
    vm.city = "";
    vm.countrys = [];
    vm.country = ''; 
    vm.towns = [];
    vm.town ='';
    vm.area = "";
    vm.villages = [];
    vm.village = "";
    $scope.loadCitys = function(){
    	CookApi.loadCitys('C51').then(function(res){
    		if(res.code == 200){
    			vm.citys = res.result.list;
    		}
    	})
    }
    $scope.$watch("cc.city",function(newValue){
        vm.country = "";
        vm.town = "";
        vm.village = "";
        if(!newValue) {
            vm.countrys = [];
            return;
        };
        CookApi.loadCitys(newValue).then(function(res){
            if(res.code == 200){
                vm.countrys = res.result.list;
            }
        });
    });
    $scope.$watch("cc.country",function(newValue){
        vm.town = "";
        vm.village = "";
        if(!newValue) {
            vm.towns = [];
            return;
        };
        CookApi.loadCitys(newValue).then(function(res){
            if(res.code == 200){
                vm.towns = res.result.list;
            }
        });
    });
    // $scope.$watch("cc.town",function(newValue){
    //     vm.village = "";
    //     if(!newValue) {
    //         vm.villages = [];
    //         return;
    //     };
    //     CookApi.loadCitys(newValue).then(function(res){
    //         if(res.code == 200){
    //             vm.villages = res.result.list;
    //         }
    //     });
    // });

    $scope.getAreas = function(){
        CookApi.getAreas().then(function(res){
            if(res.code == 200){
                vm.areas = res.result.citys;
            }
        })
    }
    
    $scope.$watch("cc.area",function(newValue){
        vm.village = "";
        if(!newValue) {
            vm.villages = [];
            return;
        };
        CookApi.loadCitys(newValue).then(function(res){
            if(res.code == 200){
                vm.villages = res.result.list;
            }
        });
    });
    $scope.loadCitys();
    $scope.getAreas();

    $scope.search = function(){
    	$scope.cooksQueryParam = {
    		page:1,
    		kw: $scope.kw,
    		area_id: vm.area ? vm.area : vm.country ,
    		subarea_id: vm.village ? vm.village: vm.town
    	}
    	$scope.cooksTable.reload();
    }
    $scope.changeStatus = function($event, cook, val){
        $event.preventDefault();
    	$("input[name='cook_"+cook.co_id+"_status']").each(function(index,item){
    		item.disabled = true;
    	});
    	CookApi.toggleCookActive({co_id: cook.co_id, status: val}).then(function(res){
    		$("input[name='cook_"+cook.co_id+"_status']").each(function(index,item){
	    		item.disabled = false;
	    	})
    		if(res.code != 200){
                sweetAlertUtil.fail(res.msg);
            }else{
                $event.target.checked = true;
            }
    	});
    }

    $scope.delCook = function(coId) {
    	sweetAlertUtil.confirm('确定删除该厨师吗？',
            function(isconfirm){
            	if(isconfirm){
            		CookApi.delCook(coId).then(function(res){
            			if(res.code == 200) {
                            $scope.cooksTable.reload();
                            sweetAlertUtil.success('删除成功');
            			}else{
            				sweetAlertUtil.fail(res.msg);
            			}
            		})
            	}
            }
        )
    }
    app.filter('rankFilter',function(){
	    return function(inputStr){
	        switch(inputStr) {
	        	case 'SUPER':
	        	  return '特级';
	        	case 'ONE':
	        	  return '一级';
	        	case 'TWO':
	        	  return '二级';
	        	case 'THREE':
	        	  return '三级';
	        	case 'FOUR':
	        	  return '四级';
	        }
	        return '';
	    }
	});
}]);